package com.ruoyi.safe.controller.api;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.safe.domain.BizEnterCheck;
import com.ruoyi.safe.domain.BizEnterCheckProject;
import com.ruoyi.safe.domain.BizEnterTask;
import com.ruoyi.safe.domain.BizProjectCheck;
import com.ruoyi.safe.domain.dto.BizEnterCheckProjectDTO;
import com.ruoyi.safe.domain.dto.EntityIdAppDTO;
import com.ruoyi.safe.domain.dto.EntityIdsAppDTO;
import com.ruoyi.safe.domain.vo.BizEnterCheckProjectVO;
import com.ruoyi.safe.service.IBizEnterCheckProjectService;
import com.ruoyi.safe.service.IBizEnterCheckService;
import com.ruoyi.safe.service.IBizEnterTaskService;
import com.ruoyi.safe.service.ICommonProjectService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 企业检查-项目Controller
 * 
 * @author ruoyi
 * @date 2023-12-06
 */
@RestController
@RequestMapping("/api/enterCheckProject/enterCheckProject")
public class EnterCheckProjectController extends BaseController
{
    @Autowired
    private IBizEnterCheckProjectService bizEnterCheckProjectService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IBizEnterTaskService bizEnterTaskService;
    @Autowired
    private IBizEnterCheckService bizEnterCheckService;
    @Autowired
    private ICommonProjectService commonProjectService;

    /**
     * 查询企业检查-项目列表
     */
    @PostMapping("/list")
    public R<Object> list(@RequestBody BizEnterCheckProjectDTO bizEnterCheckProjectDTO)
    {
        SysUser user = getSysUser(bizEnterCheckProjectDTO.getUserName());
        if (bizEnterCheckProjectDTO.getPageNum() != null && bizEnterCheckProjectDTO.getPageSize() != null) {
            PageUtils.startPage(bizEnterCheckProjectDTO.getPageNum(), bizEnterCheckProjectDTO.getPageSize());
        }
        BizEnterCheckProject bizEnterCheckProject = new BizEnterCheckProject();
        BeanUtils.copyProperties(bizEnterCheckProjectDTO, bizEnterCheckProject);
        List<BizEnterCheckProjectVO> list
                = bizEnterCheckProjectService.selectBizEnterCheckProjectList(bizEnterCheckProject, user);
        return R.ok(new PageInfo<>(list));
    }


    /**
     * 获取企业检查-项目详细信息
     */
    @PostMapping("/info")
    public AjaxResult getInfo(@RequestBody EntityIdAppDTO entityIdAppDTO)
    {
        SysUser user = getSysUser(entityIdAppDTO.getUserName());
        return success(bizEnterCheckProjectService.selectBizEnterCheckProjectById(entityIdAppDTO.getId(), user));
    }

    /**
     * 新增企业检查-项目
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BizEnterCheckProjectDTO bizEnterCheckProjectDTO)
    {
        SysUser user = getSysUser(bizEnterCheckProjectDTO.getUserName());
        BizEnterCheckProject bizEnterCheckProject = new BizEnterCheckProject();
        BeanUtils.copyProperties(bizEnterCheckProjectDTO, bizEnterCheckProject);
        bizEnterCheckProjectService.insertBizEnterCheckProject(bizEnterCheckProject, user);
        return success();
    }

    /**
     * 修改企业检查-项目
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BizEnterCheckProjectDTO bizEnterCheckProjectDTO)
    {
        SysUser user = getSysUser(bizEnterCheckProjectDTO.getUserName());
        BizEnterCheckProject bizEnterCheckProject = new BizEnterCheckProject();
        BeanUtils.copyProperties(bizEnterCheckProjectDTO, bizEnterCheckProject);
        return toAjax(bizEnterCheckProjectService.updateBizEnterCheckProject(bizEnterCheckProject, user));
    }

    /**
     * 删除企业检查-项目
     */
    @PostMapping("/delete")
    public AjaxResult remove(@RequestBody EntityIdsAppDTO entityIdsAppDTO)
    {
        getSysUser(entityIdsAppDTO.getUserName());
        return toAjax(bizEnterCheckProjectService.deleteBizEnterCheckProjectByIds(entityIdsAppDTO.getIds()));
    }

    /**
     * 修改项目自检状态为已结束
     * @param entityIdAppDTO
     * @return
     */
    @PostMapping("/updateProjectStatus")
    public AjaxResult updateProjectStatus(@RequestBody EntityIdAppDTO entityIdAppDTO){
        SysUser user = getSysUser(entityIdAppDTO.getUserName());
        BizEnterTask task = new BizEnterTask();
        task.setBizEnterCheckProjectId(entityIdAppDTO.getId());
        List<BizEnterTask> taskList = bizEnterTaskService.selectBizEnterTaskList(task);
        BizEnterTask dbTask = taskList.get(0);
        dbTask.setStatus("3");
        dbTask.setFinishTime(DateUtils.getNowDate());
        bizEnterTaskService.updateBizEnterTask(dbTask, user);

        // 如果公司检查下面的所有任务都完成了，设置公司检查状态为结束
        List<BizEnterTask> allTasks = bizEnterTaskService.selectTaskListByCheckId(dbTask.getBizEnterCheckId());
        BizEnterCheck enterCheck = bizEnterCheckService.selectBizEnterCheckById(dbTask.getBizEnterCheckId());
        if (allTasks.stream().allMatch(t -> "3".equals(t.getStatus()))) {
            enterCheck.setCheckStatus("3");
        } else {
            enterCheck.setCheckStatus("2");
        }
        bizEnterCheckService.updateBizEnterCheck(enterCheck, user);
        return success();
    }

    private SysUser getSysUser(String userName) {
        SysUser sysUser = sysUserService.selectUserByUserName(userName);
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", userName));
        }

        return sysUser;
    }

}
